var cache = {
    consumer: undefined,
    topic: undefined
}

function setConsumer(consumer) {
    cache.consumer = consumer;
    cache.topic = undefined;
    updateHash();
}

function getConsumer() {
    parserHash();
    return cache.consumer;
}

function setTopic(topic) {
    cache.topic = topic;
    updateHash();
}

function getTopic() {
    parserHash();
    return cache.topic;
}

function updateHash() {
    var hash = '';
    for (var key in cache) {
        if (cache[key] !== undefined) {
            hash += ('&' + key + '=' + cache[key]);
        }
    }
    window.location.hash = hash.length > 0 ? hash.substring(1) : hash;
}

function parserHash() {
    var qs = (location.hash.length > 0 ? location.hash.substring(1) : '');
    var items = qs.length ? qs.split("&") : [];
    for (var i = 0; i < items.length; i++) {
        var item = items[i].split("=");
        var name = decodeURIComponent(item[0]);
        var value = decodeURIComponent(item[1]);
        cache[name] = value;
    }
}

module.exports = {
    getConsumer: getConsumer,
    setConsumer: setConsumer,
    setTopic: setTopic,
    getTopic: getTopic
}